//## begin module%3B8825A101C9.cm preserve=no
//	  %X% %Q% %Z% %W%
//## end module%3B8825A101C9.cm

//## begin module%3B8825A101C9.cp preserve=no
//	+----------------------------------------------+
//	| SVMBR - A program for training SVMs          |
//	+----------------------------------------------+
//	| * Creator, modeling, coding:                 |
//	|    Marcelo Barros de Almeida                 |
//	|    barros@smar.com.br                        |
//	|    http://litc.cpdee.ufmg.br/~barros/        |
//	| * Coding, improvements, bug fixes:           |
//	|    Bernardo Penna                            |
//	|    bprc@brfree.com.br                        |
//	+----------------------------------------------+
//
//	 Copyright(c) 2002 by Marcelo Barros de Almeida
//	                            All rights reserved
//
//	-[CVS]------------------------------------------
//	$Author$
//	$Date$
//	$Source$
//	$Name$
//	$Revision$
//	------------------------------------------------
//## end module%3B8825A101C9.cp

//## Module: kernel%3B8825A101C9; Package body
//## Subsystem: <Top Level>
//## Source file: D:\textotese\svmbr\src\kernel.cpp

//## begin module%3B8825A101C9.additionalIncludes preserve=no
//## end module%3B8825A101C9.additionalIncludes

//## begin module%3B8825A101C9.includes preserve=yes
//## end module%3B8825A101C9.includes

// kernel
#include "kernel.h"
//## begin module%3B8825A101C9.declarations preserve=no
//## end module%3B8825A101C9.declarations

//## begin module%3B8825A101C9.additionalDeclarations preserve=yes
//## end module%3B8825A101C9.additionalDeclarations


// Class Kernel 




//## begin Kernel::RBF%3B880F360217.attr preserve=no  public: static SHORT {U} 1
SHORT Kernel::RBF = 1;
//## end Kernel::RBF%3B880F360217.attr

//## begin Kernel::POLY%3B880F4000DB.attr preserve=no  public: static SHORT {U} 2
SHORT Kernel::POLY = 2;
//## end Kernel::POLY%3B880F4000DB.attr

//## begin Kernel::LINEAR%3B880F520361.attr preserve=no  public: static SHORT {U} 3
SHORT Kernel::LINEAR = 3;
//## end Kernel::LINEAR%3B880F520361.attr

//## begin Kernel::SIGMOID%3B88FFA60146.attr preserve=no  public: static SHORT {U} 4
SHORT Kernel::SIGMOID = 4;
//## end Kernel::SIGMOID%3B88FFA60146.attr





// Additional Declarations
  //## begin Kernel%3B87FC9D018F.declarations preserve=yes
  //## end Kernel%3B87FC9D018F.declarations

// Class RBFKernel 

RBFKernel::RBFKernel (REAL p1v, REAL p2v, REAL p3v, Data *datap)
  //## begin RBFKernel::RBFKernel%998688394.hasinit preserve=no
  //## end RBFKernel::RBFKernel%998688394.hasinit
  //## begin RBFKernel::RBFKernel%998688394.initialization preserve=yes
  //## end RBFKernel::RBFKernel%998688394.initialization
{
  //## begin RBFKernel::RBFKernel%998688394.body preserve=yes

	p1 = p1v;
	p2 = p2v;
	p3 = p3v;
	data = datap;
	kernelType = Kernel::RBF;

  //## end RBFKernel::RBFKernel%998688394.body
}


RBFKernel::~RBFKernel()
{
  //## begin RBFKernel::~RBFKernel%.body preserve=yes
  //## end RBFKernel::~RBFKernel%.body
}



//## Other Operations (implementation)
REAL RBFKernel::evalKernel (UINT i1, UINT i2)
{
  //## begin RBFKernel::evalKernel%998688370.body preserve=yes

	// (a-b)^2 = a^2 + b^2 -2*a*b
	REAL tmp =	data->dotProd(i1,i1) + data->dotProd(i2,i2) - 
							2*data->dotProd(i1,i2);

	//  exp(-|x1-x2|^2/(2*p3*p1^2))
	return exp(-tmp/(2*p1*p1*p3));

  //## end RBFKernel::evalKernel%998688370.body
}

REAL RBFKernel::evalKernel (REAL *vec, UINT i2)
{
  //## begin RBFKernel::evalKernel%999139410.body preserve=yes

	REAL tmp =	data->dotProd(vec,vec) + data->dotProd(i2,i2) - 
							2*data->dotProd(vec,i2);
	return exp(-tmp/(2*p1*p1*p3));

  //## end RBFKernel::evalKernel%999139410.body
}

// Additional Declarations
  //## begin RBFKernel%3B87FC7102F4.declarations preserve=yes
  //## end RBFKernel%3B87FC7102F4.declarations

// Class SigmoidKernel 

SigmoidKernel::SigmoidKernel (REAL p1v, REAL p2v, REAL p3v, Data *datap)
  //## begin SigmoidKernel::SigmoidKernel%998688395.hasinit preserve=no
  //## end SigmoidKernel::SigmoidKernel%998688395.hasinit
  //## begin SigmoidKernel::SigmoidKernel%998688395.initialization preserve=yes
  //## end SigmoidKernel::SigmoidKernel%998688395.initialization
{
  //## begin SigmoidKernel::SigmoidKernel%998688395.body preserve=yes

	p1 = p1v;
	p2 = p2v;
	p3 = p3v;
	data = datap;
	kernelType = Kernel::SIGMOID;

  //## end SigmoidKernel::SigmoidKernel%998688395.body
}


SigmoidKernel::~SigmoidKernel()
{
  //## begin SigmoidKernel::~SigmoidKernel%.body preserve=yes
  //## end SigmoidKernel::~SigmoidKernel%.body
}



//## Other Operations (implementation)
REAL SigmoidKernel::evalKernel (UINT i1, UINT i2)
{
  //## begin SigmoidKernel::evalKernel%998688371.body preserve=yes

	// tanh(p1*x1*x2/p3+p2)
	REAL tmp = data->dotProd(i1,i2);
	tmp = p1*tmp/p3 + p2;
  return (1+exp(tmp))/(1-exp(tmp));

  //## end SigmoidKernel::evalKernel%998688371.body
}

REAL SigmoidKernel::evalKernel (REAL *vec, UINT i2)
{
  //## begin SigmoidKernel::evalKernel%999139411.body preserve=yes

	REAL tmp = data->dotProd(vec,i2);
	tmp = p1*tmp/p3 + p2;
  return (1+exp(tmp))/(1-exp(tmp));
  	
  //## end SigmoidKernel::evalKernel%999139411.body
}

// Additional Declarations
  //## begin SigmoidKernel%3B87FCA5024E.declarations preserve=yes
  //## end SigmoidKernel%3B87FCA5024E.declarations

// Class LinearKernel 

LinearKernel::LinearKernel (REAL p1v, REAL p2v, REAL p3v, Data *datap)
  //## begin LinearKernel::LinearKernel%998688396.hasinit preserve=no
  //## end LinearKernel::LinearKernel%998688396.hasinit
  //## begin LinearKernel::LinearKernel%998688396.initialization preserve=yes
  //## end LinearKernel::LinearKernel%998688396.initialization
{
  //## begin LinearKernel::LinearKernel%998688396.body preserve=yes

	p1 = p1v;
	p2 = p2v;
	p3 = p3v;
	data = datap;
	kernelType = Kernel::LINEAR;  

  //## end LinearKernel::LinearKernel%998688396.body
}


LinearKernel::~LinearKernel()
{
  //## begin LinearKernel::~LinearKernel%.body preserve=yes
  //## end LinearKernel::~LinearKernel%.body
}



//## Other Operations (implementation)
REAL LinearKernel::evalKernel (UINT i1, UINT i2)
{
  //## begin LinearKernel::evalKernel%998688372.body preserve=yes

  // (x1*x2+p2)/p3
	REAL tmp = data->dotProd(i1,i2);
	return (tmp + p2)/p3;
	
  //## end LinearKernel::evalKernel%998688372.body
}

REAL LinearKernel::evalKernel (REAL *vec, UINT i2)
{
  //## begin LinearKernel::evalKernel%999139412.body preserve=yes

	REAL tmp = data->dotProd(vec,i2);
	return (tmp + p2)/p3;  
	
  //## end LinearKernel::evalKernel%999139412.body
}

// Additional Declarations
  //## begin LinearKernel%3B87FCB90026.declarations preserve=yes
  //## end LinearKernel%3B87FCB90026.declarations

// Class PolyKernel 

PolyKernel::PolyKernel (REAL p1v, REAL p2v, REAL p3v, Data *datap)
  //## begin PolyKernel::PolyKernel%998688397.hasinit preserve=no
  //## end PolyKernel::PolyKernel%998688397.hasinit
  //## begin PolyKernel::PolyKernel%998688397.initialization preserve=yes
  //## end PolyKernel::PolyKernel%998688397.initialization
{
  //## begin PolyKernel::PolyKernel%998688397.body preserve=yes

 	p1 = p1v;
	p2 = p2v;
	p3 = p3v;
	data = datap;
	kernelType = Kernel::POLY;

  //## end PolyKernel::PolyKernel%998688397.body
}


PolyKernel::~PolyKernel()
{
  //## begin PolyKernel::~PolyKernel%.body preserve=yes
  //## end PolyKernel::~PolyKernel%.body
}



//## Other Operations (implementation)
REAL PolyKernel::evalKernel (UINT i1, UINT i2)
{
  //## begin PolyKernel::evalKernel%998688373.body preserve=yes

	// ((x1*x2+p2)/p3)^p1
	REAL tmp = data->dotProd(i1,i2);
	return pow((tmp + p2)/p3,p1);

  //## end PolyKernel::evalKernel%998688373.body
}

REAL PolyKernel::evalKernel (REAL *vec, UINT i2)
{
  //## begin PolyKernel::evalKernel%999139413.body preserve=yes
	
	REAL tmp = data->dotProd(vec,i2);
	return pow((tmp + p2)/p3,p1);

  //## end PolyKernel::evalKernel%999139413.body
}

// Additional Declarations
  //## begin PolyKernel%3B87FCC40086.declarations preserve=yes
  //## end PolyKernel%3B87FCC40086.declarations

//## begin module%3B8825A101C9.epilog preserve=yes
//## end module%3B8825A101C9.epilog
